import {
  get,
  postParam
} from "../../utils/service"
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify'
import {
  getUserInfo
} from "../../utils/common"

var app = getApp()

Page({

  /**
   * 页面的初始数
   */
  data: {
    userName: "xiaop0817",
    password: "123456",
    loading: false,
    errorMsg: null
  },
  onUserNameChange: function (e) {
    this.setData({
      userName: e.detail
    })
  },
  onPaswordChange: function (e) {
    this.setData({
      password: e.detail
    })
  },
  onLogin: function () {
    console.info(this.data.userName, this.data.password)
    if (!(this.data.userName || this.data.password)) {
      Notify({
        type: 'warning',
        message: '请填写用户名和密码'
      })
      return
    }

    this.setData({
      loading: true
    })
    const self = this
    postParam(`${app.globalData.host}/auth/login`, {
      userName: this.data.userName,
      password: this.data.password
    }, {
      success: function (data) {
        app.globalData.token = data.token
        wx.setStorageSync('token', data.token)
        getUserInfo({
          success: function () {
            wx.switchTab({
              url: '/pages/library/library',
            })
            // wx.navigateTo({
            //   url: '/pages/address/address',
            // })
            // wx.navigateTo({
            //   url: '/pages/order/order',
            // })
          }
        })
      },
      fail: function () {

      },
      complete: function () {
        self.setData({
          loading: false
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})